package com.example.demo.saToken.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName TokenController
 * @Description Sa-Token会话管理控制器（支持前端获取登录状态和退出登录）
 * @Author System
 * @Date 2025/9/25
 **/
@SaCheckLogin
@RestController
@RequestMapping("/acc")
@Tag(name = "会话管理", description = "Sa-Token会话管理相关接口")
public class TokenController {

    @GetMapping("/tokenInfo")
    @Operation(summary = "获取Token信息", description = "获取当前登录用户的Token详细信息")
    public SaResult tokenInfo() {
        String tokenInfo = JSONUtil.toJsonStr(StpUtil.getTokenInfo());
        return SaResult.data(URLEncodeUtil.encode(tokenInfo));
    }

    @PostMapping("/logout")
    @Operation(summary = "用户退出", description = "退出当前用户登录状态")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok("退出成功");
    }
}
